<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <<<<<<< HEAD
    
    <link rel="stylesheet" href="./layui.css">
=======
>>>>>>> 201653db3ad33c6b5d68dafa147a03a88c01cc0b
</head>
<body>
    <h1></h1>
    <div class="content"></div>
    <div class="comment">
        <textarea class="comment_content" style="width: 100%; height: 200px;"></textarea><br/>
<<<<<<< HEAD
        <button type="button" class="send_comment layui-btn layui-btn-primary">发布</button>
=======
        <button type="button" class="send_comment">发布</button>
>>>>>>> 201653db3ad33c6b5d68dafa147a03a88c01cc0b
    </div>
    <div class="comments">  
    </div>

<script src="../7月15日js系列周五/com.js"></script>
<script>
    let id = getUrlParam('id');

     document.querySelector('.comment_content').value= localStorage.getItem('content')
  
    let userInfo = getCookie('userInfo')?JSON.parse(getCookie('userInfo')):null;

    ajax({
        url:'http://phpclub.org.cn/edu/server/getContent.php',
        data:{id},
        success:function(response){
            document.querySelector('.layui-nav').innerHTML = response.data.title;
            document.querySelector('.content').innerHTML = response.data.content;
        }
    })

    document.querySelector('.send_comment').onclick = function(){
     
        if(!userInfo){
           
            localStorage.setItem('content',document.querySelector('.comment_content').value);
       
            let back = encodeURIComponent(location.href);
            location.href = 'login.html?back='+back;
        }else{
            ajax({
                url:'http://phpclub.org.cn/edu/server/addComment.php',
                type:'post',
                data:{
                    userId:userInfo.id,
                    newsId:id,
                    content:document.querySelector('.comment_content').value
                },
                success:function(response){
                    localStorage.removeItem('content');
                    alert('发布评论成功！等待审核后显示');
                }
            })
        }
    }

  
    ajax({
        url:'http://phpclub.org.cn/edu/server/getComment.php',
  
        data:{newsId:id,userId:userInfo?userInfo.id:0},
        success: function(response){
            let html = '';
            response.data.forEach(item=>{
                html+=`<div class="user_comment" data-commentid="${item.id}">
                    <div class="user_comment_content">${item.content}</div>
                    <div class="user"><p>${item.user.mobile}</p>
<<<<<<< HEAD
                    ${item.hasLike == 0 ?'<button class="good layui-btn layui-btn-primary">点赞</button>':''}
                    ${userInfo && item.user_id  == userInfo.id?'<button class="delete layui-btn layui-btn-primary">删除</button>':''}    
=======
                    ${item.hasLike == 0 ?'<button class="good">点赞</button>':''}
                    ${userInfo && item.user_id  == userInfo.id?'<button class="delete">删除</button>':''}    
>>>>>>> 201653db3ad33c6b5d68dafa147a03a88c01cc0b
                    </div>
                </div>
                <hr/>`;
            })
            document.querySelector('.comments').innerHTML = html;
        }
    });


    document.querySelector('.comments').addEventListener('click',function(event){
        if(event.target.className=='good'){
           
            if(!userInfo){
                
                let back = encodeURIComponent(location.href);
                location.href = 'login.html?back='+back;
            }else{
                ajax({
                    url:'http://phpclub.org.cn/edu/server/commentLike.php',
                    type:'post',
                    data:{userId:userInfo.id,commentId:event.target.parentElement.parentElement.getAttribute('data-commentid')},
                    success: function(backData){
                     
                 event.target.disabled = true;
                    }
                });
            }
        }else if(event.target.className == 'delete'){
         
            ajax({
                url:'http://phpclub.org.cn/edu/server/deleteComment.php',
                data:{userId:userInfo.id,commentId:event.target.parentElement.parentElement.getAttribute('data-commentid')},
                success: function(backData){
                    
                    document.querySelector('.comments').removeChild(event.target.parentElement.parentElement);
                }
            });
        }
    })


</script>
<script src="./layui.js"></script>
</body>
</html>